package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @GetMapping("page")
    public Result<PageResult> getPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("{}", dishPageQueryDTO);
        PageResult pageResult = dishService.getPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("")
    public Result<Object> add(@RequestBody DishDTO dishDTO){
        log.info("{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }

    @DeleteMapping
    public Result<Object> delete(@RequestParam List<Long> ids){
        log.info("{}",ids);
        dishService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result<DishVO> getById(@PathVariable Long id){
      DishVO dishVo   =dishService.getById(id);
      return Result.success(dishVo);
    }

    @PutMapping("")
    public Result<Object> update(@RequestBody DishDTO dishDTO){
        log.info("{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return Result.success();
    }

    @PostMapping("status/{status}")
    public Result<Object> setStatus(@PathVariable Integer status, Long id){
        log.info("status:{},id:{}",status,id);
        Dish dish=new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishService.setStatus(dish);
        return Result.success();
    }

    @GetMapping("list")
    public Result<List<Dish>> getDishList(Long categoryId){
        log.info("categoryId:{}",categoryId);
       List<Dish> list= dishService.getDishListByCategoryId(categoryId);
       return Result.success(list);
    }

}
